package com.nytd.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

@RequestMapping("/r2")
@Controller
public class Response {

    @RequestMapping("/index1")
    @ResponseBody
    public Object index1(){
        return "/index.html";
    }

    @RequestMapping("/index2")
    public Object index2(){
        return "/index.html";
    }

    @RequestMapping("/getHtml")
    @ResponseBody
    public String returnHtml() {
        return "<h1>hello, html<h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson1")
    public HashMap<String, String> returnJson1(){
        HashMap<String, String> map = new HashMap<>();
        map.put("java", "value1");
        map.put("c++", "value2");
        map.put("python", "value3");
        return map;
    }

    @RequestMapping("/setStatus")
    @ResponseBody
    public String setStatus(HttpServletResponse response){
        response.setStatus(404);
        return "设置状态码成功";
    }

    @RequestMapping(value = "/returnJson2", produces = "test/plain; charset = utf-8")
    @ResponseBody
    public String returnJson2(){
        return "{\"success\": true}";
    }

    @RequestMapping("/setHeader")
    @ResponseBody
    public String setHeader(HttpServletResponse response){
        response.setHeader("java", "value");
        return "设置header成功";
    }

    @RequestMapping("/setContentTpe")
    @ResponseBody
    public void setContentType(HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.getOutputStream().write("设置状态码成功".getBytes(StandardCharsets.UTF_8));
    }


}
